package com.ruoyi.system.service.impl;

import java.util.Date;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.SuperSearchHistoryMapper;
import com.ruoyi.system.domain.SuperSearchHistory;
import com.ruoyi.system.service.ISuperSearchHistoryService;

/**
 * 搜索历史Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-08-13
 */
@Service
public class SuperSearchHistoryServiceImpl implements ISuperSearchHistoryService
{
    @Autowired
    private SuperSearchHistoryMapper superSearchHistoryMapper;

    /**
     * 查询搜索历史
     * 
     * @param id 搜索历史主键
     * @return 搜索历史
     */
    @Override
    public SuperSearchHistory selectSuperSearchHistoryById(Long id)
    {
        return superSearchHistoryMapper.selectSuperSearchHistoryById(id);
    }

    /**
     * 查询搜索历史列表
     * 
     * @param superSearchHistory 搜索历史
     * @return 搜索历史
     */
    @Override
    public List<SuperSearchHistory> selectSuperSearchHistoryList(SuperSearchHistory superSearchHistory)
    {
        return superSearchHistoryMapper.selectSuperSearchHistoryList(superSearchHistory);
    }

    /**
     * 新增搜索历史
     * 
     * @param superSearchHistory 搜索历史
     * @return 结果
     */
    @Override
    public int insertSuperSearchHistory(SuperSearchHistory superSearchHistory)
    {
        // 如果已经存在则不再添加直接返回成功
        List<SuperSearchHistory> list = superSearchHistoryMapper.selectSuperSearchHistoryList(null);
        for (SuperSearchHistory item : list) {
            if (item.getKeyword().equals(superSearchHistory.getKeyword())) {
                return 1;
            }
        }
        // 返回添加结果
        superSearchHistory.setSearchedAt(new Date());
        superSearchHistory.setDel(0);
        return superSearchHistoryMapper.insertSuperSearchHistory(superSearchHistory);
    }

    /**
     * 修改搜索历史
     * 
     * @param superSearchHistory 搜索历史
     * @return 结果
     */
    @Override
    public int updateSuperSearchHistory(SuperSearchHistory superSearchHistory)
    {
        return superSearchHistoryMapper.updateSuperSearchHistory(superSearchHistory);
    }

    /**
     * 批量删除搜索历史
     * 
     * @param ids 需要删除的搜索历史主键
     * @return 结果
     */
    @Override
    public int deleteSuperSearchHistoryByIds(Long[] ids)
    {
        return superSearchHistoryMapper.deleteSuperSearchHistoryByIds(ids);
    }

    /**
     * 删除搜索历史信息
     * 
     * @param id 搜索历史主键
     * @return 结果
     */
    @Override
    public int deleteSuperSearchHistoryById(Long id)
    {
        return superSearchHistoryMapper.deleteSuperSearchHistoryById(id);
    }

    // 根据用户id删除用户的搜索记录
    @Override
    public int deleteByUserId(Long userId) {
        return superSearchHistoryMapper.deleteByUserId(userId);
    }
}
